package com.eqdd.yiqidian.ui.gongneng.renshi.quanxian;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.DepartmentListResultBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.test.ExpandBean;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 17-11-14  上午10:45.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class SelectBumenExpandActivity extends BaseActivity {

    private RecyclerViewCustom dataBinding;
    private ArrayList<DepartmentListResultBean> data;
    private SlimAdapterEx<DepartmentListResultBean> slimAdapterEx;
    private ArrayList<DepartmentListResultBean> departmentListResultBeans;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "部门");
        initTopRightText("确定", v -> {
            getChosenDeparts();
            if (departmentListResultBeans != null && departmentListResultBeans.size() > 0) {
                Intent intent = new Intent();
                intent.putParcelableArrayListExtra("list", departmentListResultBeans);
                setResult(Config.SUCCESS, intent);
                finish();
            } else {
                ToastUtil.showShort("至少选择一个部门");
            }
        });
    }

    private void getChosenDeparts() {
        departmentListResultBeans = new ArrayList<>();
        Flowable.fromIterable(slimAdapterEx.getData())
                .filter(ExpandBean::isCheck)
                .subscribe(departmentListResultBeans::add);

    }

    @Override
    public void initData() {

        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_72, new SlimInjector<DepartmentListResultBean>() {
            @Override
            public void onInject(DepartmentListResultBean data, IViewInjector injector) {

                injector.text(R.id.tv_title, data.getDepartName())
                        .visibility(R.id.checkbox, data.isTouch() ? View.VISIBLE : View.GONE)
                        .checked(R.id.checkbox,data.isCheck())
                        .with(R.id.iv_left, view -> {
                            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                            layoutParams.leftMargin = 50 * data.getLevel();
                            view.setLayoutParams(layoutParams);
                        });
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    DepartmentListResultBean dataItem = slimAdapterEx.getDataItem(position);
                    if (dataItem.isTouch()) {
                        dataItem.setCheck(!dataItem.isCheck());
                        slimAdapterEx.notifyItemChanged(position);
                    }
                });
    }

    @Override
    public void setView() {

        data = new ArrayList<>();
        Flowable<HttpResult<List<DepartmentListResultBean>>> allDepartFlow = OkGo.<HttpResult<List<DepartmentListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.Select_SECTION)
                .params("CompanyId", AppConstent.user.getCompanyId())
                .params("ParentId", 0)
                .converter(new JsonConverter<HttpResult<List<DepartmentListResultBean>>>() {
                    @Override
                    public void test() {
                        super.test();
                    }
                })
                .adapt(new FlowableBody<HttpResult<List<DepartmentListResultBean>>>());
        Flowable<HttpResult<List<DepartList>>> unchooseDepartFlow = OkGo.<HttpResult<List<DepartList>>>post(HttpConfig.BASE_URL + HttpConfig.SET_DIMISSION_LEADER_DEPARTMENT)
                .params("companyId", AppConstent.user.getCompanyId())
                .converter(new JsonConverter<HttpResult<List<DepartList>>>() {
                    @Override
                    public void test() {
                        super.test();
                    }
                })
                .adapt(new FlowableBody<HttpResult<List<DepartList>>>());
        Flowable.combineLatest(allDepartFlow, unchooseDepartFlow, (listHttpResult, listHttpResult2) -> {
            initDepart(listHttpResult.getItems(), listHttpResult2.getItems(), 0);
            return 1;
        }).subscribe(integer -> {
            slimAdapterEx.updateData(data);
        });

    }

    private void initDepart(List<DepartmentListResultBean> items, List<DepartList> items1, int deep) {
        if (items == null || items.size() == 0) {
            return;
        } else {
            for (int i = 0; i < items.size(); i++) {
                DepartmentListResultBean departmentListResultBean = items.get(i);
                departmentListResultBean.setLevel(deep);
                for (DepartList departList : items1) {
                    if (departmentListResultBean.getDepartId().equals(departList.getDepartId() + "")) {
                        departmentListResultBean.setTouch(false);
                        break;
                    }
                }
                data.add(departmentListResultBean);
                initDepart(departmentListResultBean.getChilds(), items1, deep + 1);
            }
        }
    }


    public static class DepartList {

        /**
         * departId : 251
         * department : 研讨部
         * user : null
         */

        private int departId;
        private String department;
        private String user;

        public int getDepartId() {
            return departId;
        }

        public void setDepartId(int departId) {
            this.departId = departId;
        }

        public String getDepartment() {
            return department;
        }

        public void setDepartment(String department) {
            this.department = department;
        }

        public Object getUser() {
            return user;
        }

        public void setUser(String user) {
            this.user = user;
        }
    }

}
